<template>
  <div>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工艺编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入工艺编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工艺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺版本" prop="artVersion">
        <el-input
          v-model="queryParams.artVersion"
          placeholder="请输入工艺版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺文件" prop="documentId">
        <el-input
          v-model="queryParams.documentId"
          placeholder="请输入工艺文件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺指导书" prop="guidBookId">
        <el-input
          v-model="queryParams.guidBookId"
          placeholder="请输入工艺指导书"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请输入启用状态" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyiyinzheng:artTable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyiyinzheng:artTable:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyiyinzheng:artTable:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyiyinzheng:artTable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      max-height="250"
      highlight-current-row
      @current-change="handleCurrentChange"
      v-loading="loading" :data="artTableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="工艺编号" align="center" prop="code" />
      <el-table-column label="工艺名称" align="center" prop="name" />
      <el-table-column label="工艺版本" align="center" prop="artVersion" />
      <el-table-column label="工艺文件" align="center" prop="documentName" />
      <el-table-column label="工艺指导书" align="center" prop="guidName" />
      <el-table-column label="启用" align="center" prop="enable" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enable"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['ruoyiyinzheng:artTable:edit', 'system:oss:download']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">工艺</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddArtTable" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownload" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['ruoyiyinzheng:artTable:edit', 'system:oss:download']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">指导</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddArtGuidTable" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownArtGuidload" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyiyinzheng:artTable:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyiyinzheng:artTable:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工艺编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入工艺编号" />
        </el-form-item>
        <el-form-item label="工艺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工艺名称" />
        </el-form-item>
        <el-form-item label="工艺版本" prop="artVersion">
          <el-input v-model="form.artVersion" placeholder="请输入工艺版本" />
        </el-form-item>

        <el-form-item label="启用" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改OSS对象存储对话框 -->
    <el-dialog :title="title" :visible.sync="openFile" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名">
          <fileUpload v-model="form.file" @fileUploadSuccess="updateFileSuccess" v-if="type === 0"/>
          <imageUpload v-model="form.file" v-if="type === 1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
    <div>
      <ArtProcessRelation ref="childRef" :process-list="processList"></ArtProcessRelation>>
    </div>
  </div>
</template>

<script>
import { listArtTable, getArtTable, delArtTable, addArtTable, updateArtTable } from "@/api/ruoyiyinzheng/artTable";
import ArtProcessRelation from "@/views/ruoyiyinzheng/artProcessRelation/index.vue";
import { listProcess} from "@/api/ruoyiyinzheng/process";
export default {
  dicts: ['sys_yes_no'],
  components:{ArtProcessRelation},
  name: "ArtTable",
  data() {
    return {
      processList:[],
      openWindowMark:0,
      type:0,
      openFile:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺表格数据
      artTableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        name: undefined,
        artVersion: undefined,
        documentId: undefined,
        guidBookId: undefined,
        documentName: undefined,
        guidName: undefined,
        enable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "工艺编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "工艺名称不能为空", trigger: "blur" }
        ],
        request: [
          { required: true, message: "工艺版本不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getProcessList();
  },
  methods: {
    getProcessList()
    {
      listProcess().then(response => {
        this.processList =  response.rows.map(item => ({
          id: item.id,
          name: item.name,
        }));
      });
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      const child_ref=  this.$refs.childRef;
      child_ref.queryParams={artName:val.id}
      child_ref.artID=val.id;
      child_ref.handleQuery();
      console.log(val)
    },
    /** 查询工艺列表 */
    updateFileSuccess(data)
    {
      // fileName
      //   :
      //   "cc.txt"
      // ossId
      //   :
      //   "1704292588473499650"
      // url
      //   :
      //   "http://127.0.0.1:9000/ruoyi/2023/09/20/9b8b2392d58a432abdc661ac15c29857.txt"
      if(this.openWindowMark==1)
      {
        this.form.documentId=data.ossId;
      }else if(this.openWindowMark==2)
      {
        this.form.guidBookId=data.ossId;
      }
      this.openWindowMark=0;
      console.log(data);
    },
    handleCommand(command, row) {
      const id = row.id || this.ids
      getArtTable(id).then(response => {
        this.form = response.data;
      });
      switch (command) {
        case "handleAddArtTable":
          this.handleAddArtTable(row);
          break;
        case "handleDownload":
          this.handleDownload(row);
          break;
        case "handleDownArtGuidload":
          this.handleDownArtGuidload(row);
          break;
        case "handleAddArtGuidTable":
          this.handleAddArtGuidTable(row);
          break;
        default:
          break;
      }
    },
    subSuccessFileUpload()
    {
      this.openFile=false;
    },
    /** 下载按钮操作 */
    handleDownArtGuidload(row) {
      this.$download.oss(row.guidBookId);
    },
    handleAddArtGuidTable(row)
    {
      this.openWindowMark=2;
      this.openFile=true;
    },
    /** 下载按钮操作 */
    handleDownload(row) {
      this.$download.oss(row.documentId)
    },
    handleAddArtTable(row)
    {
      this.openWindowMark=1;
      this.openFile=true;
    },
    getList() {
      this.loading = true;
      listArtTable(this.queryParams).then(response => {
        this.artTableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 文件按钮操作 */
    handleFile() {
      this.reset();
      this.openFile = true;
      this.title = "上传文件";
      this.type = 0;
    },
    /** 图片按钮操作 */
    handleImage() {
      this.reset();
      this.openFile = true;
      this.title = "上传图片";
      this.type = 1;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        name: undefined,
        artVersion: undefined,
        documentId: undefined,
        guidBookId: undefined,
        documentName: undefined,
        guidName: undefined,
        enable: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getArtTable(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工艺";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateArtTable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addArtTable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工艺编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delArtTable(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyiyinzheng/artTable/export', {
        ...this.queryParams
      }, `artTable_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
